<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/8/14
 * Time: 15:40
 */

namespace App\Service;


use App\Models\FloorRoom;

class FloorRoomService
{
    /**
     * 获取楼房列表
     * @param array $params
     * @param string $orderBy
     * @param string $sort
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public static function getList($params = [],$orderBy = 'id',$sort = 'DESC',$page,$limit)
    {
        $where = [];
//      $floorRoom = new FloorRoom();
        if(isset($params['name']) && !empty(intval($params['name']))){
            $where[] = ['name','like',"%{$params['name']}%"];
        }
        if(isset($params['building_no']) && !empty(intval($params['building_no']))){
            $where[] = ['building_no','=',intval($params['building_no'])];
        }
        if(isset($params['floor_no']) && !empty(intval($params['floor_no']))){
            $where[] = ['floor_no','=',intval($params['floor_no'])];
        }
        if(isset($params['room_no']) && !empty(intval($params['room_no']))){
            $where[] = ['room_no','=',intval($params['room_no'])];
        }
        $start=($page-1)*$limit;
        $list = FloorRoom::where($where)->skip($start)->take($limit)->orderBy($orderBy,$sort)->get()->toArray();
        $count = FloorRoom::where($where)->count();
        $pageCount = ceil($count/$limit);
        $data = [
            'list'          => $list,
            'pageCount'     => $pageCount,
        ];

        return Response()->json(['code'=>200,'msg'=>'获取成功','data'=>$data]);
    }

    /**
     * 通过ID获取楼房信息
     * @param int $memberId
     * @return array
     */
    public static function getFloorRoomById($memberId = 0)
    {
        $info = FloorRoom::where('id', $memberId)->first(['id', 'name', 'building_no', 'floor_no', 'room_no','order', 'created_at', 'updated_at']);
        $info = $info ? $info->toArray() : [];
        return Response()->json(['code' => 200, 'msg' => '获取成功', 'data' => $info]);
    }

    /**
     * 添加
     * @param array $params
     * @return \Illuminate\Http\JsonResponse
     */
    public static function add($params = [])
    {
        try{
            if(intval($params['floor_no']) > 99999 ){
                throw new \Exception('公寓栋数不能大于99999');
            }
            if(intval($params['floor_no']) > 1000 ){
                throw new \Exception('楼层数不能大于1000');
            }
            $floorRoomInfo = FloorRoom::where('building_no',intval($params['building_no']))->where('floor_no',intval($params['floor_no']))->where('room_no',trim($params['room_no']))->first();
            if($floorRoomInfo){
                throw new \Exception('房间号已存在，添加失败');
            }
            $info = FloorRoom::create(['name'=>trim($params['name']),'building_no'=>intval($params['building_no']),'floor_no'=>intval($params['floor_no']),'room_no'=>trim($params['room_no']),'order'=>intval($params['order'])]);
            if(!$info){
                throw new \Exception('新增失败');
            }
        }catch (\Exception $e){
            return Response()->json(['code'=>201,'msg'=>$e->getMessage()]);
        }
        return Response()->json(['code'=>200,'msg'=>'新增成功']);
    }
    
    public static function addBatch($params = [])
    {
        return Response()->json(['code'=>201,'msg'=>'不支持批量添加']);
        $buildingNum = 101;
        $insertData = [];
        $time = date('Y-m-d H:i:s');
        $num = 3306;
        for($i = 100;$i <= $buildingNum; $i++){
            if($i == 100){
                $floorNum = 6;
            }else{
                $floorNum = 12;
            }
            for($j = 1; $j <= $floorNum; $j ++){
                if($i == 100){
                    $name = '教师公寓';
                }else{
                    $name = '培训大楼';
                }
                $kNum = 36;
                
                for($k = 1;$k <= $kNum;$k ++){
                    $num ++;
                    $insertData[] = [
                        'name'          => $name,
                        'building_no'   => $i,
                        'floor_no'      => $j,
                        'room_no'       => $j.substr(strval($k+100),1,2),
                        'order'         => $num,
                        'created_at'    => $time,
                        'updated_at'    => $time,
                    ];
                }
                
            }
        }
        dd($insertData);
        $insert = FloorRoom::insert($insertData);
        if(!$insert){
            return Response()->json(['code'=>201,'msg'=>'新增失败']);
        }
        return Response()->json(['code'=>200,'msg'=>'新增成功']);
        
    }
    
    public static function addBatchBak($params = [])
    {
        return Response()->json(['code'=>201,'msg'=>'不支持批量添加']);
        $buildingNum = 12;
        $floorNum = 6;
        $insertData = [];
        $time = date('Y-m-d H:i:s');
        $num = 0;
        for($i = 1;$i <= $buildingNum; $i++){
            for($j = 1; $j <= $floorNum; $j ++){
                if($i == 1){
                    $kNum = 72;
                }elseif($i == 2){
                    $kNum = 119;
                }else{
                    $kNum = 36;
                }
                for($k = 1;$k <= $kNum;$k ++){
                    $num ++;
                    $insertData[] = [
                        'name'          => '第 '.$i.' 栋',
                        'building_no'   => $i,
                        'floor_no'      => $j,
                        'room_no'       => $j.substr(strval($k+1000),1,3),
                        'order'         => $num,
                        'created_at'    => $time,
                        'updated_at'    => $time,
                    ];
                }
                
            }
        }
        $insert = FloorRoom::insert($insertData);
        if(!$insert){
            return Response()->json(['code'=>201,'msg'=>'新增失败']);
        }
        return Response()->json(['code'=>200,'msg'=>'新增成功']);
        
    }

    /**
     * 修改
     * @param array $params
     * @return \Illuminate\Http\JsonResponse
     */
    public static function edit($params = [])
    {
        try{
            if(intval($params['floor_no']) > 99999 ){
                throw new \Exception('公寓栋数不能大于99999');
            }
            if(intval($params['floor_no']) > 1000 ){
                throw new \Exception('楼层数不能大于1000');
            }
            $info = FloorRoom::where('id',intval($params['id']))->first();
            if(!$info){
                throw new \Exception('楼房不存在');
            }
            if(FloorRoom::where('building_no',intval($params['building_no']))->where('floor_no',intval($params['floor_no']))->where('room_no',trim($params['room_no']))->where('id','!=',intval($params['id']))->first()){
                throw new \Exception('房间号已存在，添加失败');
            }
            $info->name = trim($params['name']);
            $info->building_no = intval($params['building_no']);
            $info->floor_no = intval($params['floor_no']);
            $info->room_no = trim($params['room_no']);
            $info->order = intval($params['order']);
            $save = $info->save();
            if(!$save){
                throw new \Exception('修改失败');
            }
        }catch (\Exception $e){
            return Response()->json(['code'=>201,'msg'=>$e->getMessage()]);
        }
        return Response()->json(['code'=>200,'msg'=>'修改成功']);
    }

    /**
     * 删除
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public static function destroy($id)
    {
        $idArr = is_array($id) ? $id : [$id];
        if(!$idArr){
            return Response()->json(['code'=>201,'msg'=>'请选择要删除的楼房']);
        }
        $del = FloorRoom::destroy($idArr);
        if($del){
            return Response()->json(['code'=>200,'msg'=>'删除成功']);
        }
        return Response()->json(['code'=>201,'msg'=>'删除失败']);
    }

    /**
     * 获取所有的栋数号
     * @return \Illuminate\Http\JsonResponse
     */
    public static function getAllBuilding($params = [])
    { 
        $where = [];
        if(isset($params['status']) && !empty(intval($params['status']))){
            $where[] = ['status','=',1];
        }
        $buildingArr = FloorRoom::where($where)->orderBy('order','ASC')->pluck('building_no');
        $buildingArr = $buildingArr ? array_unique($buildingArr->toArray()) : [];
        sort($buildingArr);
        return Response()->json(['code'=>200,'msg'=>'获取成功','data'=>$buildingArr]);
    }

    /**
     * 获取全部的楼层号
     * @param array $params
     * @return \Illuminate\Http\JsonResponse
     */
    public static function getAllFloor($params = [])
    {
        $where = [];
        if(isset($params['building_no']) && !empty(intval($params['building_no']))){
            $where[] = ['building_no','=',intval($params['building_no'])];
        }
        if(isset($params['status']) && !empty(intval($params['status']))){
            $where[] = ['status','=',1];
        }
        $floorArr = FloorRoom::where($where)->orderBy('order','ASC')->pluck('floor_no');
        $floorArr = $floorArr ? array_unique($floorArr->toArray()) : [];
        sort($floorArr);
        return Response()->json(['code'=>200,'msg'=>'获取成功','data'=>$floorArr]);
    }

    /**
     * 获取全部的房间编号
     * @param $params
     * @return \Illuminate\Http\JsonResponse
     */
    public static function getAllRoom($params)
    {
        $where = [];
        if(isset($params['building_no']) && !empty(intval($params['building_no']))){
            $where[] = ['building_no','=',intval($params['building_no'])];
        }
        if(isset($params['floor_no']) && !empty(intval($params['floor_no']))){
            $where[] = ['floor_no','=',intval($params['floor_no'])];
        }
        if(isset($params['status']) && !empty(intval($params['status']))){
            $where[] = ['status','=',1];
        }
        $floorArr = FloorRoom::where($where)->orderBy('order','ASC')->get(['id','building_no','floor_no','room_no'])->toArray();
        sort($floorArr);
        return Response()->json(['code'=>200,'msg'=>'获取成功','data'=>$floorArr]);
    }

}
